package com.star.common.orm.config;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.autoconfigure.MybatisPlusProperties;
import com.baomidou.mybatisplus.core.MybatisConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.star.common.orm.plugin.FieldAutoFillHandler;
import com.star.common.orm.plugin.InsertIgnorePlugin;
import com.star.common.orm.typehandlers.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandlerRegistry;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;

/**
 * Mybatis 配置
 *
 * @author zhouhengzhe
 **/
@Slf4j
@Configuration(proxyBeanMethods = false)
@RequiredArgsConstructor
@Component
public class MybatisPlusConfig implements InitializingBean {
    private final MybatisPlusProperties mybatisPlusProperties;
    private final List<JsonStringTypeHandler> jsonStringTypeHandlers;

    @Bean
    public MybatisPlusInterceptor paginationInterceptor() {
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }

    /**
     * INSERT IGNORE 改写插件
     */
    @Bean
    @ConditionalOnMissingBean
    public InsertIgnorePlugin insertIgnorePlugin() {
        return new InsertIgnorePlugin();
    }

    /**
     * INSERT、UPDATE自动填充处理器
     */
    @Bean
    @ConditionalOnMissingBean
    public MetaObjectHandler fieldAutoFillHandler() {
        log.info("### BASE INIT : fieldAutoFillHandler ###");
        return new FieldAutoFillHandler();
    }

    @Override
    public void afterPropertiesSet() {
        MybatisConfiguration configuration = mybatisPlusProperties.getConfiguration();
        TypeHandlerRegistry typeHandlerRegistry = configuration.getTypeHandlerRegistry();
        typeHandlerRegistry.register(BigDecimal.class, new BigDecimalTypeHandler());
        typeHandlerRegistry.register(JdbcType.DECIMAL, new BigDecimalTypeHandler());
        typeHandlerRegistry.register(String[].class, new StringsTypeHandler());
        typeHandlerRegistry.register(Long[].class, new LongsTypeHandler());
        typeHandlerRegistry.register(Integer[].class, new IntegersTypeHandler());
        // JavaObject、JavaArray
        if (CollUtil.isNotEmpty(jsonStringTypeHandlers)) {
            for (JsonStringTypeHandler jsonStringTypeHandler : jsonStringTypeHandlers) {
                typeHandlerRegistry.register(jsonStringTypeHandler.type(), jsonStringTypeHandler);
            }
        }
    }

}
